// JavaScript Document
$(function(){
	$("#periodTable .del").livequery('click', function(event){
//	$("#respTables .del").click(function() {   
        $(this).parents(".repeat").remove(); 
		return false; 
    }); 
		
//	$("#periodTable .add").click(function(){  
	$("#periodTable .add").livequery('click', function(event){
		//1td
		var fromtimeInput = $("<input type='text' class='the_text timepicker' name='gmtStart' style='width:120px;'/>");
		fromtimeInput.datetimepicker();
		
		var firstTD = $('<td colspan="2"></td>');
		fromtimeInput.appendTo(firstTD);
		
		var thisTR = $('<tr class="repeat"></tr>')
		firstTD.appendTo(thisTR);

		//2td
		var totimeInput = $("<input type='text' class='the_text timepicker' name='gmtEnd' style='width:120px;'/>");
		totimeInput.datetimepicker();
		
		var secondTD = $('<td colspan="2"></td>');
		totimeInput.appendTo(secondTD);
		
		secondTD.appendTo(thisTR);
		
		//3td
		var third2TD = $('<td colspan="2"><input type="text" id="fault_minutes" name="fault_minutes" class="the_text" style="width:60px;" value=""></td>');
		third2TD.appendTo(thisTR);
		
		//4td
		var thirdTD = $('<td><select id="fault_level" name="fault_level"><option value="0">事故级</option><option value="1">A类</option><option value="2">B类</option><option value="3">C类</option></select></td>');
		
		thirdTD.appendTo(thisTR);
		
		//5td
		var fourthTD = $('<td align="center"><img src="casestudy/images/16add.jpg" align="absmiddle" title="add" class="add"><img src="casestudy/images/16del.jpg" align="absmiddle" title="del"  class="del" style="display:none"></td>');
		fourthTD.appendTo(thisTR);
         

		//tr
		thisTR.appendTo($('#periodTable')); 
		$(this).next().show(); 
		$(this).remove();
		fromtimeInput.change(function(){
			var cur = $(this);
			if(cur.attr("name") == 'gmtStart'){
				var start = cur;
				var end = cur.parent().next().children();
			}else if(cur.attr("name") == 'gmtEnd'){
				var start = cur.parent().prev().children();
				var end = cur;
			}
			var startTime = start.val();
			var endTime = end.val();
			if(startTime && endTime){
				var next = cur.parent().next().children().val();
	    		var startDate = new Date(startTime);
	    		var endDate = new Date(endTime);
	    		//alert(endDate.getTimezoneOffset() - startDate.getTimezoneOffset())
	    		var startMin = startDate.getTime();
	    		var endMin = endDate.getTime();
	    		//alert((endMin - startMin) / 60000);
	    		if((endMin - startMin) >= 0){
	    			end.parent().next().children().val((endMin - startMin) / 60000);
	    		}else{
	    			end.parent().next().children().val((endMin - startMin) / 60000);
	    			//alert("写错了吧！");
	    		}
			}
		});
		
		totimeInput.change(function(){
			var cur = $(this);
			if(cur.attr("name") == 'gmtStart'){
				var start = cur;
				var end = cur.parent().next().children();
			}else if(cur.attr("name") == 'gmtEnd'){
				var start = cur.parent().prev().children();
				var end = cur;
			}
			var startTime = start.val();
			var endTime = end.val();
			if(startTime && endTime){
				var next = cur.parent().next().children().val();
	    		var startDate = new Date(startTime);
	    		var endDate = new Date(endTime);
	    		//alert(endDate.getTimezoneOffset() - startDate.getTimezoneOffset())
	    		var startMin = startDate.getTime();
	    		var endMin = endDate.getTime();
	    		//alert((endMin - startMin) / 60000);
	    		if((endMin - startMin) >= 0){
	    			end.parent().next().children().val((endMin - startMin) / 60000);
	    		}else{
	    			end.parent().next().children().val((endMin - startMin) / 60000);
	    			//alert("写错了吧！");
	    		}
			}
		});
		
		return false; 
    });  	
});